package com.gavin.bootstrap;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class FileController {
	@Value("${web.upload-path}")
    private String path;

	@RequestMapping(method = RequestMethod.POST, value = "/testUploadFile")
	@ResponseBody
	public String handleFileUpload(@RequestParam("upfile") MultipartFile file) {
		System.out.println("11");
		System.out.println( file.getOriginalFilename());
		if (!file.isEmpty()) {
			try {
				BufferedOutputStream out = new BufferedOutputStream(
						new FileOutputStream(new File(path + file.getOriginalFilename())));
				out.write(file.getBytes());
				out.flush();
				out.close();

			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return "上传失败," + e.getMessage();
			} catch (IOException e) {
				e.printStackTrace();
				return "上传失败," + e.getMessage();
			}
		} else {
			return "上传失败，因为文件是空的.";
		}

		return "redirect";
	}
}
